package net.sureon.biz;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import net.sureon.app.AppContext;
import net.sureon.common.exception.SureonException;
import net.sureon.common.mail.MailData;
import net.sureon.common.mail.MailProcessor;
import net.sureon.dao.Company;
import net.sureon.dao.User;

public class TitaonMailHelper {
	private static TitaonMailHelper instance = new TitaonMailHelper();
	public static final String MACRO_COMPAY_VALID_DATE = "%companyValidDate%";
	public static final String MACRO_USERNAME = "%userName%";
	public static final String MACRO_REAL_NAME = "%realName%";
	public static final String MACRO_SITEURL = "%siteURL%";
	public static final String MACRO_INIT_PASSWORD = "%initPassword%";
	public static final String MACRO_SALES_NAME = "%SalesName%";
	public static final String MACRO_SALES_TEL_NUMBER = "%SalesTelNumber%";
	public static final String MACRO_BILLING_DATE = "%billingDate%";
	public static final String MACRO_YEAR = "%Year%";
	public static final String MACRO_MONTH = "%Month%";
	public static final String MACRO_N_SUBMIT_DAYS = "%nSubmitDays%";
	public static final String MACRO_LAST_SUBMIT = "%lastSubmitDate%";
	public static final String MACRO_N_AUUTO_APPROVAL = "%nAutoApprove%";
	public static final String MACRO_N_AUTO_APPROVE_DAYS = "%nNoSubmitDays%";
	public static final String MACRO_COMPANY_NAME = "%companyName%";
	public static final String MACRO_TO_BE_APPROVED_RECORDS = "%toBeApprovedRecords%";
	public static final String MACRO_TOTALUSERNUMBER = "%totalUserNumber%";
	public static final String MACRO_FROZEUSERNUMBER = "%frozeUserNumber%";
	public static final String MACRO_NEWUSERNUMBER = "%newUserNumber%";
	public static final String MACRO_BILLUSERNUMBER = "%billUserNumber%";
	public static final String MACRO_PRICE = "%price%";
	public static final String MACRO_TOTALAMOUNT = "%totalAmount%";
	public static final String MACRO_LATESTAMOUNT = "%latestAmount%";
	public static final String MACRO_BALANCE = "%balance%";
	public static final String MACRO_BILLINGDATE = "%billingDate%";
	
	public static final String MAIL_NAME_ACCOUT_ACTIVATED = "user_account_activated";
	public static final String MAIL_NAME_ACCOUNT_DEACTIVATED = "user_account_deactivated";
	public static final String MAIL_NAME_ACCOUNT_CREATED = "user_account_created";
	public static final String MAIL_NAME_REQUIRE_SUBMIT_DAILYTIMIG = "require_submit_dailytiming";
	public static final String MAIL_NAME_REQUIRED_APPROVE = "required_approve_applications";
	public static final String MAIL_NAME_RESET_PASSWORD = "reset_user_password";
	public static final String MAIL_NAME_CONSUME_RECORD = "consume_record";
	public static final String MAIL_NAME_BILLING_REMINDER = "billing_reminder";
	public static final SimpleDateFormat reminderDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	public static final String MAIL_NAME_COMPANY_ACCOUT_OPEND = "company_trail_account_opened";

	public static TitaonMailHelper getInstance() {
		return instance;
	}

	private TitaonMailHelper() {

	}

	/**
	 * generate macro from system level and company level
	 * 
	 * @param company
	 * @return
	 */
	public Map generateCompanyMacro(CompanyBO company) {
		Map map = new HashMap();
		Company vo = company.getCompanyVO();
		map.put(MACRO_COMPAY_VALID_DATE, vo.getValidDate());
		map.put(MACRO_SITEURL, vo.getSiteURL());
		map.put(MACRO_COMPANY_NAME, vo.getCompanyFullName());
		map.put(MACRO_SALES_NAME, "Zheng Liang");
		map.put(MACRO_SALES_TEL_NUMBER, "021-1234567");
		return map;
	}

	public Map generateUserMacro(UserBO user) {
		Map map = new HashMap();
		User vo = user.getUserVO();
		map.put(MACRO_USERNAME, vo.getUserName());
		map.put(MACRO_REAL_NAME, vo.getRealName());
		return map;
	}
	
	public void sendUserReminderMail(String mailName, CompanyBO company, UserBO user, String initPassword) throws SureonException{
		Map replacementMap = TitaonMailHelper.getInstance().generateCompanyMacro(company);
		replacementMap.putAll(TitaonMailHelper.getInstance().generateUserMacro(user));
		if(initPassword != null){
			replacementMap.put(TitaonMailHelper.MACRO_INIT_PASSWORD, initPassword);
		}
		MailData data = MailData.defaulMailData();
		data.setReplacementMap(replacementMap);
		data.setRecipients(new String[]{user.getUserVO().getEmail()});
		MailProcessor.getInstance().processMail(mailName, data, mailName, AppContext.getLocale());
	}
}
